<template>
  <div class="group-select">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-checkbox :indeterminate="isIndeterminate"
        v-model="checkAll" @change="handleCheckAllChange" :label="options.name"></el-checkbox>
      </div>
      <el-row>
        <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
            <el-col :span="8" v-for="city in cities" :key="city.id">
              <el-checkbox :label="city.id" :key="city.id">{{ city.name }}</el-checkbox>
            </el-col>
        </el-checkbox-group>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GroupSelect',
  props: ['options'],
  data() {
    return {
      checkAll: false,
      checkedCities: this.options.resourceList
        .filter((menu) => menu.selected)
        .map((menu) => menu.id),
      cities: this.options.resourceList,
      isIndeterminate: true,
    };
  },
  computed: {
    cityOptions() {
      return this.options.resourceList.map((menu) => menu.id);
    },
  },
  methods: {
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.cityOptions : [];
      this.isIndeterminate = false;
      this.$emit('handleChange', this.options.id, this.checkedCities);
    },
    handleCheckedCitiesChange(value) {
      const checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
      this.$emit('handleChange', this.options.id, value);
    },
  },

};
</script>
